﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using com.code.Editors.Code;

namespace com.Editors.UserControls
{
    public partial class ColumnCode : UserControl
    {
        int width = 3;
        int height = 3;
        int x = 3;
        int y = 13;

        System.Windows.Forms.TableLayoutPanel panel;
        System.Windows.Forms.Label lblColName;
        System.Windows.Forms.CheckBox chkCol;
        System.Windows.Forms.CheckBox chkColIsDetail;
        System.Windows.Forms.ComboBox cmbCol;
        System.Windows.Forms.Button btnDataBind;
        System.Windows.Forms.Button btnJScript;

        UIClientScript uiObj = null;
        UIAddDatasource uiObjDS = null;
        GuidDataColumn colObj = null;
        IServiceProvider provider = null;

        internal GuidDataColumn DataColObj
        {
            get { return colObj; }
            set { colObj = value; }
        }

        public ColumnCode(GuidDataColumn column, IServiceProvider provider)
        {
            InitializeComponent();
            this.provider = provider;
            //colObj = new GuidDataColumn(colName z);
            colObj = column;
            this.Controls.Add(this.addNewColumnRow(column.ColName));
        }

        private TableLayoutPanel addNewColumnRow(string colName)
        {
            
            // panel
            //
            panel = new System.Windows.Forms.TableLayoutPanel();
            chkCol = new System.Windows.Forms.CheckBox();
            chkColIsDetail = new System.Windows.Forms.CheckBox();
            lblColName = new System.Windows.Forms.Label();
            btnDataBind = new System.Windows.Forms.Button();
            btnJScript = new System.Windows.Forms.Button();
            cmbCol = new ComboBox();

            panel.Name = "panel" + colName;
            panel.Size = new System.Drawing.Size(900, 30);
            panel.Location = new System.Drawing.Point(3, 20);
            panel.AutoSize = true;
            panel.AutoSizeMode = AutoSizeMode.GrowOnly;
            panel.ColumnCount = 6;

            //this.t1Panel1.TabIndex = 0;
            // 
            // chkT1Panel1
            // 
            chkCol.AutoSize = true;
            chkCol.Name = "chk" + colName;
            chkCol.Checked = true;
            chkCol.Text = "";
            chkCol.UseVisualStyleBackColor = true;
            chkCol.CheckedChanged += new EventHandler(chkIsChecked_CheckChange);
            panel.Controls.Add(chkCol);
            // 
            // t1Panel1lblColName
            //
            //lblColName.Location.X
            lblColName.AutoSize = true;
            lblColName.Name = "lbl" + colName;
            lblColName.Size = new System.Drawing.Size(35, 13);
            //lblColName.Location = new System.Drawing.Point(x+10, width);
            lblColName.Text = "" + colName;
            panel.Controls.Add(lblColName);

            cmbCol.Font = new Font(FontFamily.GenericSansSerif, 7);
            cmbCol.Height = cmbCol.Height - 10;
 
            this.fillControlType(cmbCol);
            cmbCol.SelectedIndexChanged += new EventHandler(cmbUIControlType_Selected);
            panel.Controls.Add(cmbCol);

            chkCol.AutoSize = true;
            chkCol.Name = "chkDetail" + colName;
            chkCol.Text = "";
            chkCol.UseVisualStyleBackColor = true;
            panel.Controls.Add(chkColIsDetail);

            //t1Panel1.Controls.Add(panel);

            btnDataBind.Text = "Data Bind";
            btnDataBind.Click += new EventHandler(btnDataBind_Click);
            panel.Controls.Add(btnDataBind);

            btnJScript.Text = "ClientScript";
            btnJScript.Click += new EventHandler(btnUIScript_Click);
            panel.Controls.Add(btnJScript);

            width += 30;
            x += 10;

            return panel;
        }

        private void btnUIScript_Click(object sender, EventArgs e)
        {
            uiObj = new UIClientScript(colObj, btnJScript.Location);
            colObj.IsUIScript = true;
            colObj.ObjClientScript = uiObj;
            uiObj.Show();
        }

        private void btnDataBind_Click(object sender, EventArgs e)
        {
            uiObjDS = new UIAddDatasource(colObj, provider, btnDataBind.Location);
            colObj.IsBindData = true;
            colObj.ObjUIDatasource = uiObjDS;
            uiObjDS.Show();
        }

        private void cmbUIControlType_Selected(object sender, EventArgs e)
        {
            colObj.UiControlType = cmbCol.Text;
        }

        private void chkIsChecked_CheckChange(object sender, EventArgs e)
        {
            colObj.IsChecked = chkCol.Checked;
            
            
        }

        private void fillControlType(ComboBox cmb)
        {
            cmb.Items.Clear();
            cmb.Items.Add("---Select---");
            cmb.Items.Add("TextBox");
            cmb.Items.Add("DropDownList");
            cmb.Items.Add("CheckBox");
            cmb.Items.Add("RadioButton");
            cmb.Items.Add("HyperLink");
            cmb.Items.Add("Label");
            cmb.Items.Add("HtmlHidden");
            cmb.Items.Add("TextBox");
            cmb.Items.Add("TextBox");
            cmb.SelectedIndex = 1;
            colObj.UiControlType = cmb.Text;
        }

        public bool IsChecked()
        {
            return chkCol.Checked;
        }

        public bool IsDetail()
        {
            return chkColIsDetail.Checked;
        }

        public bool IsAddBindData()
        {
            return (this.uiObjDS != null);
        }
        public bool IsAddUIScript()
        {
            return (this.uiObj != null);
        }

        public string GetColUIControlType()
        {
            return cmbCol.Text;
        }

        
    }
}
